﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

using palmApp.Database;

namespace palmApp
{
    public partial class machineScreen : Form
    {
        static String comtec_id_machine;
        static String comtec_id_owner;
        
        public machineScreen()
        {
            InitializeComponent();

            this.ShowDialog();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            statusBar1.Text = "Ingresar nueva mantención para máquina";
            menuItem1.Text = "Ingresar";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            statusBar1.Text = "Ver historial mantención de la máquina";
            menuItem1.Text = "Ingresar";
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void machineScreen_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

            if (e.KeyCode.ToString().ToUpper() == "N")
            {
                radioButton1.Checked = true;
            }

            if (e.KeyCode.ToString().ToUpper() == "H")
            {
                radioButton2.Checked = true;
            }
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            radioButton2.Checked = true;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            machineScreen_KeyDown(sender, e);
        }
        
        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (menuItem1.Text == "Conectar")
            {
                String tagmachine = "F1BD2C";
                
                Manager dbManager = new Manager();

                DataSet ds = dbManager.operationMethod();

                DataTable machines  = ds.Tables["machines"];
                DataTable owner     = ds.Tables["owner"];
                DataTable operation = ds.Tables["operation"];

                foreach(DataRow drCurrent in machines.Rows)
                {
                    if (drCurrent["tag_id_machine"].ToString().Equals(tagmachine))
                    {
                        comtec_id_machine   = drCurrent["comtec_id_machine"].ToString();
                        comtec_id_owner     = drCurrent["comtec_id_owner"].ToString();
                        this.textBox1.Text  = drCurrent["tag_id_machine"].ToString();
                        this.textBox2.Text  = drCurrent["cod_mainteiner"].ToString();
                        this.textBox3.Text  = drCurrent["brand"].ToString();
                        this.textBox4.Text  = drCurrent["model"].ToString();
                        this.textBox5.Text  = drCurrent["year"].ToString();
                        this.textBox10.Text = drCurrent["cod_owner"].ToString();
                    }
                }

                foreach(DataRow drCurrent in owner.Rows)
                {
                    if (drCurrent["comtec_id_owner"].ToString().Equals(comtec_id_owner))
                    {
                        this.textBox6.Text = drCurrent["contact"].ToString();
                        this.textBox8.Text = drCurrent["company_name"].ToString();
                        this.textBox9.Text = drCurrent["comtec_id_owner"].ToString();
                    }
                }

                if (operation.Rows.Count == 0)
                {
                    foreach (DataRow drCurrent in operation.Rows)
                    {
                        if (drCurrent["comtec_id_machine"].ToString().Equals(comtec_id_machine))
                        {

                        }
                    }
                }
            }

            if (radioButton1.Checked)
            {
                maintenanceScreen nuevaman = new maintenanceScreen(Convert.ToInt32(comtec_id_machine));
                //radioButton1.Focus();
                radioButton1.Checked = false;
                statusBar1.Text = "";
                menuItem1.Text = "Conectar";
            }

            if (radioButton2.Checked)
            {
                historyScreen history = new historyScreen(Convert.ToInt32(comtec_id_machine));
                //radioButton1.Focus();
                radioButton2.Checked = false;
                statusBar1.Text = "";
                menuItem1.Text = "Conectar";
                
            }
            
        }

        private void machineScreen_Load(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_ParentChanged(object sender, EventArgs e)
        {

        }

        private void label17_ParentChanged(object sender, EventArgs e)
        {

        }

        private void panel1_GotFocus(object sender, EventArgs e)
        {

        }
    }
}